#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import sys
import glob
import shutil
import datetime
import schedule
import time 
import logging
from subprocess import Popen, PIPE
from functools import wraps

log_file = '/data/logs/upload_py.log'
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(levelname)s %(message)s',
                    datefmt='%Y%m%d %H:%M:%S',
                    filename=log_file,
                    filemode='ab+')

def logging_(func):
  @wraps(func)
  def wrapper(*args, **kw):
    ret = func(*args, **kw)
    logging.info(func.__name__ + "Successfully")
    return result
  return wrapper

src = '/data/backup/'
upload_log_dir = '/data/logs/appgallery_china/'
log_path = '/data/logs/save_log/'
cmd_py = 'ansible AppgalleryVisitLog -m shell -a "python /apps/script/copy_log.py"'
cmd_pull_log = 'ansible AppgalleryVisitLog -m synchronize -a "src={src} dest={log_path} mode=pull"'.format(src=src, log_path=log_path)
cmd_cos = 'coscmd upload -rs {upload_log_dir} /appgallery_china/'.format(upload_log_dir=upload_log_dir)
cmd_push_hd = 'rsync -az {upload_log_dir} hadoop@ip:/data/archlog/homepage/appgallery_china/'.format(upload_log_dir=upload_log_dir)

T_dir = upload_log_dir + datetime.datetime.now().strftime('%Y/%m/')
if not os.path.exists(T_dir):
  os.makedirs(T_dir)

def Shell(cmd):
    if isinstance(cmd, str):
      cmd_list = cmd.split()
      cmd_shell = Popen(cmd_list, stdout=PIPE, stderr=PIPE)
      return cmd_shell.poll()
    else:
      print("Please enter a string command!")
      return None

#Collect remote node logs
@logging_
def collection_log():
  cmd1, cmd2 = Shell(cmd_py), Shell(cmd_pull_log)
  return True if 0 in (cmd1, cmd2) else sys.exit(1)

#log mv upload_dir
@logging_
def mv_log():
  loger = glob.iglob(log_path + '*')
  for logs in loger:
    y_time, m_time, d_time = logs.split(".")[-1].split("-")
    time_dir = ''.join(list([upload_log_dir, y_time, '/', m_time, '/', d_time, '/']))
    if not os.path.exists(time_dir):
      os.makedirs(time_dir)
    shutil.copy2(logs, time_dir)

  return True

#log upload cos and push hadoop
@logging_
def upload_cos():
  cmd3, cmd4 = Shell(cmd_cos), Shell(cmd_push_hd)
  return True if 0 in (cmd3, cmd4) else sys.exit(2)

@logging_
def main():
  collection_log()
  mv_log()    
  upload_cos()
  
if __name__ == '__main__':
  schedule.every().day.at("00:00").do(main)
  while 1:
      schedule.run_pending()
      time.sleep(1)
